package com.pine.app.module.security.oauth.filter;

import com.pine.app.module.security.core.Authentication;
import com.pine.app.module.security.core.matcher.RequestMatcher;
import com.pine.app.module.security.oauth.exception.AuthenticationException;
import com.pine.app.module.security.oauth.provider.sso.token.OAuth2SsoTokenHandler;
import com.pine.app.module.security.oauth.provider.token.OAuth2AccessToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *  单点登陆处理器
 * @author xiaoyuan
 * @create 2020/3/17 17:41
 **/
@Order(50)
@Slf4j
public class OAuth2ClientAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter {




//    public OAuth2ClientAuthenticationProcessingFilter(RequestMatcher requestMatcher, AuthenticationSessionStore authenticationSessionStore, OAuth2SsoTokenHandler oAuth2ClientRequest, RequestCache requestCache) {
//        super(requestMatcher, authenticationSessionStore, oAuth2ClientRequest, requestCache);
//    }



    @Override
    public void setRequestMatcher(RequestMatcher requestMatcher) {
        super.setRequestMatcher(requestMatcher);
    }
    @Autowired
    @Override
    public void setoAuth2ClientRequest(OAuth2SsoTokenHandler oAuth2ClientRequest) {
        super.setoAuth2ClientRequest(oAuth2ClientRequest);
    }

    @Override
    public Authentication authentication(HttpServletRequest request, HttpServletResponse response) throws IOException {
        OAuth2AccessToken accessToken;
        try {
            accessToken = oAuth2ClientRequest.getAccessToken(request, response);
        } catch (AuthenticationException e) {
            throw e;
        }
        if (accessToken == null) {
            return null;
        }
        return oAuth2ClientRequest.getOAuth2Authentication(accessToken);
    }
}
